package com.three_learn.star.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.three_learn.star.system.entity.sys_user;
import com.three_learn.star.system.mapper.SysUserMapper;
import com.three_learn.star.system.po.DTO.SysUserDTO;
import com.three_learn.star.system.po.VO.SysUserVO;
import com.three_learn.star.system.po.query.UserQuery;
import com.three_learn.star.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;



/*
 *描述 ：
 *Author:28768
 */
@Service
public class ISysUserServiceImpl extends ServiceImpl<SysUserMapper, sys_user> implements ISysUserService {

     @Resource
     private SysUserMapper sysUserMapper;
     //根据id查询用户信息
     @Override
     public SysUserVO getUserInfo(Long id) {
          QueryWrapper<sys_user> Wrapper = new QueryWrapper<>();
          Wrapper.eq("id",id);
          sys_user sysUser = sysUserMapper.selectOne(Wrapper);
          SysUserVO sysUserVO = new SysUserVO();
          BeanUtil.copyProperties(sysUser,sysUserVO);
          return sysUserVO;
     }

     //批量删除
     @Override
     public void deleteByIds(List<Long> ids) {
          for(Long id:ids){
               sysUserMapper.deleteById(id);
          }
     }

     //添加用户
     @Override
     public void addUserInfo(SysUserDTO sysUserDTO) {
          sys_user sysUser = new sys_user();
          BeanUtil.copyProperties(sysUserDTO,sysUser);
          sysUser.setTenantId(1L);
          sysUser.setCreateTime(LocalDateTime.now());
          sysUserMapper.insert(sysUser);

     }
     //根据页码和每页显示条数查询所有用户信息
     @Override
     public List<SysUserVO> queryAll(Integer page, Integer pageSize) {
          //分页基本条件
          Page<sys_user> pageQuery = new Page<>(page, pageSize);
          QueryWrapper<sys_user> queryWrapper = new QueryWrapper<>();
          //查询额外条件
          Page<sys_user> sysUserPage = sysUserMapper.selectPage(pageQuery, queryWrapper);
          return  sysUserPage.getRecords().stream().map(user->{
               SysUserVO sysUserVO = new SysUserVO();
               BeanUtil.copyProperties(user,sysUserVO);
               return sysUserVO;
          }).collect(Collectors.toList());
     }

     //条件分页查询
     @Override
     public List<SysUserVO> queryAll1(Integer page, Integer pageSize, UserQuery userQuery) {
          String code=userQuery.getCode();
          String userName=userQuery.getUserName();
          String nickName=userQuery.getNickName();
          String phone=userQuery.getPhone();
          String status=userQuery.getStatus();
          //分页基本条件
          Page<sys_user> pageQuery = new Page<>(page, pageSize);
          LambdaQueryWrapper<sys_user> queryWrapper = new LambdaQueryWrapper<>();
          queryWrapper.eq(code!=null&&!"".equals(code),sys_user::getCode,code)
                  .like(userName!=null&&!"".equals(userName),sys_user::getUserName,userName)
                  .eq(nickName!=null&&!"".equals(nickName),sys_user::getNickName,nickName)
                  .eq(phone!=null&&!"".equals(phone),sys_user::getPhone,phone)
                  .eq(userQuery!=null&&userQuery.getSex()!=null,sys_user::getSex,userQuery.getSex())
                  .eq(status!=null&&!"".equals(status),sys_user::getStatus,status);
          //查询额外条件
          Page<sys_user> sysUserPage = sysUserMapper.selectPage(pageQuery, queryWrapper);
          return  sysUserPage.getRecords().stream().map(user->{
               SysUserVO sysUserVO = new SysUserVO();
               BeanUtil.copyProperties(user,sysUserVO);
               return sysUserVO;
          }).collect(Collectors.toList());
     }

     //修改用户
     @Override
     public void updateById(SysUserDTO sysUserDTO) {
          sys_user sysUser = new sys_user();
          BeanUtil.copyProperties(sysUserDTO,sysUser);
          sysUser.setUpdateTime(LocalDateTime.now());
          sysUserMapper.updateById(sysUser);
     }


}
